<template>

    <div class="wrapper">
        <div class="header">
            <el-row class="name" style="margin-bottom:10px">
                账号名称：{{userInfo.name}}
            </el-row>
             <el-row>
                 <el-col :span="8">
                      <el-form style="width:300px" ref="passForm" :model="passForm" :rules="rule" label-position="left" label-width="100px">
                        <el-form-item label="修改密码" prop='password'>
                            <el-input v-model="passForm.password" show-password  placeholder="请输入密码"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" prop='psd'>
                            <el-input v-model="passForm.psd" show-password  placeholder="请再输入密码"></el-input>
                        </el-form-item>
                    
                        <el-button style="width:80px" @click="changePsd" type="danger">确定</el-button>
                    
                    </el-form>
                 </el-col>
                 <!-- <el-col :span="10" style="margin-top:40px">
                            （拥有账号管理权限的用户才可以看到下面列表）
                 </el-col> -->
                 
            </el-row>
        </div>
        <div v-if="flag">
            <div class=searchBox>
                <el-row style="margin-bottom:20px">
                    <el-button type="danger" style="width:120px" @click="centerDialogVisible=true;title='新增'">新建</el-button>
                </el-row>
                <el-row>
                    <el-col :span="4" style="margin-right:30px">
                        <el-input v-model="searchBo.userName" placeholder="请输入用户名" clearable></el-input>
                    </el-col> <el-button type="danger" @click="getList" style="width:120px">查询</el-button>
                </el-row>
            </div>
            <div class="body">
                <Table :list="list" :state="false" v-loading="loading" :total="total"
                    @sizeChange="handSize"
                    @currentChange="currentChange">
                    <slot>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button
                                    size="mini"
                                    type="primary"
                                    @click="handleEdit(scope.row)">修改
                                </el-button>
                                <el-button
                                    size="mini"
                                    type="danger"
                                    @click="handleDel(scope.row)">删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </slot>
                </Table>
            </div>
            <el-dialog
                :title="title"
                 :before-close="onCancel"
                :visible.sync="centerDialogVisible"
                width="30%"
                >
                <div>
                    <el-form ref="addForm" :model="addForm" :rules="rule" label-position="right" label-width="140px">
                        <el-form-item label="用户名" prop='name'>
                            <el-input v-model="addForm.name" placeholder="请输入用户名"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop='password'>
                            <el-input show v-model="addForm.password" show-password placeholder="请输入密码"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" prop='psd1'>
                            <el-input v-model="addForm.psd1" show-password placeholder="请再次确认密码"></el-input>
                        </el-form-item>
                           <el-form-item label="用户角色" prop='roleId'>
                                 <el-select clearable v-model="addForm.roleId" placeholder="请选择用户角色">
                                    <el-option
                                    v-for="item in role"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                                    </el-option>
                                </el-select>
                            <!-- <el-input  v-model="addForm.roleId" placeholder="请选择用户角色"></el-input> -->
                        </el-form-item>
                    </el-form>
                </div>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="onCancel">取 消</el-button>
                    <el-button type="primary" @click="onOk">确 定</el-button>
                </span>
            </el-dialog>
         </div>
    </div>
</template>

<script>
import {accountEdit,accountDel,
accountList,accountAdd,
changePSD,roleList

} from '../../../api/setting'
import Table from '@/components/Table'
import Cookies from 'js-cookie'
export default {
    components:{
        Table
    },
    data () {
        return {
            userInfo:{},
            flag :false,
            list:{
                thead:[
                    // {label:'采集器编号',prop:"index",rowspan:1},
                    {label:'用户名称',prop:"name",rowspan:1},
                    {label:'用户角色',prop:"roleName",rowspan:1},
                    {label:'用户权限',prop:"authorityList",rowspan:1,formatter:(row,col)=>{
                        let list = row.authorityList.map(item=>{
                            return item.name
                        })
                        return list.join(',')
                    }},
                    {label:'备注',prop:"remark",rowspan:1},
                ],
                tbody:[]
            },
            loading:false,
            total:0,
            title:'新增',
            searchBo:{},
            centerDialogVisible:false,
            addForm:{
                name:'',
                id:'',
                remark:''
            },
            passForm:{
                password:'',
                psd:''
            }, 
            role:[],
            rule:{
                name:[  { required: true, message: '请输入指令簇名称', trigger: 'blur' },],
                id:[  { required: true, message: '请输入指令簇编码', trigger: 'blur' },],
                remark:[  { required: true, message: '请输入备注', trigger: 'blur' },],
                password:[{ required: true, message: '请输入密码', trigger: 'blur' },{
                    validator:(rule,value,callback)=>{
                        let reg= /^[a-zA-Z]\w{5,17}$/
                        if(!reg.test(value)){callback(new Error('请以字母开头，最少6位密码'))
                        }else{
                            callback()
                        }
                    },trigger: 'blur'
                }],
                psd:[{ required: true, message: '请再次输入密码', trigger: 'blur' },
                {
                    validator:(rule,value,callback)=>{
                        if(value !== this.passForm.password){
                            callback(new Error('两次密码不一致!'))
                        }else{
                            callback()
                        }
                    }
                }],
                   psd1:[{ required: true, message: '请再次输入密码', trigger: 'blur' },
                {
                    validator:(rule,value,callback)=>{
                        if(value !== this.addForm.password){
                            callback(new Error('两次密码不一致!'))
                        }else{
                            callback()
                        }
                    }
                }]
            },
            pageSize:20,
            pageNum:1,
            total:0,
            instructionClusterId:''

        }
        
    },
    mounted () {
        this.getList()
        this.userInfo = JSON.parse(Cookies.get('userInfo'))
        this.flag = this.userInfo.authorityList.some(item=>{
            return item.code === 'USER_MANAGEMENT'
        })
        roleList({pageSize:0,pageNum:1}).then(res=>{
            if(res.status ==200){
                this.role  = res.data.list;
            }
        })
    },
    methods:{
        changePsd () {
            console.log(this.$md5('e10adc3949ba59abbe56e057f20f883e'))
            this.$refs['passForm'].validate((vaild)=>{
                if(vaild){
                    changePSD({password:this.$md5(this.passForm.password)}).then(res=>{
                        if(res.status == 200) {
                            this.$message({
                                type:'success',
                                message:'修改成功！'
                            })
                           
                        }else{
                                this.$message.error(res.msg)
                            }
                    }).finally(res=>{
                         this.passForm = this.$options.data().passForm
                    })
                }
            })
        },
        onOk () {
            this.$refs['addForm'].validate((vaild)=>{
                if(vaild) {
                     let obj ={...this.addForm}
                            obj.password = this.$md5(obj.password)
                    if(this.title == '新增'){
                       
                        accountAdd(obj).then(res=>{
                            if(res.status == 200) {
                                this.$message({
                                    type:'success',
                                    message:"新增成功！"
                                })
                                this.getList()
                                this.onCancel()
                            }else{
                                this.$message.error(res.msg)
                            }
                        })
                    }else{
                         accountEdit(obj).then(res=>{
                            if(res.status == 200) {
                                this.$message({
                                    type:'success',
                                    message:"修改成功！"
                                })
                                this.getList()
                                this.onCancel()
                            }else{
                                this.$message.error(res.msg)
                            }
                        })
                    }
                }
            })
           
        },
        onCancel () {
            this.centerDialogVisible = false;
            this.addForm=this.$options.data().addForm
            this.$refs['addForm'].clearValidate();
            this.$refs['addForm'].resetFields();
        },
        getList () {
            let obj = {
                pageNum:this.pageNum,
                pageSize:this.pageSize,
                ...this.searchBo
            }
            accountList(obj).then(res=>{
                if(res.status == 200) {
                    this.total = res.data.total;
                    this.list.tbody = res.data.list;
                }
            })
        },
        handSize (val) {
            this.pageNum = val;
            this.getList()
        },
        currentChange(val) {
            this.pageSize = val;
            this.getList()
        },
        handleEdit (row) {
            this.title = '修改'
            this.addForm = {...row};
            console.log(row)
            this.addForm.psd = row.password;
            this.centerDialogVisible = true;
        },
        handleDel (row) {
             this.$confirm('此操作将永久删除该项, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                    accountDel(row).then(res=>{
                        if(res.data) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.getList()
                        }else{
                            this.$message({
                                type:'error',
                                message:res.msg
                            })
                        }
                    })
                    
                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        }
    }
}
</script>

<style lang="less" scoped>
    .wrapper{
        width: 100%;
        overflow: auto;
        color:#606266;
        .header{
            height: 250px;
            margin-bottom: 25px;
            background-color: #fff;
            padding:0 20px;
            box-sizing: border-box;;
            .name{
                width:100%;
                height: 50px;
                padding-top:20px;
                box-sizing: border-box;;
                border-bottom: 2px solid #ccc;
                
            }
        }
        .searchBox {
            width:100%;
            height: 144px;;
            background-color: #fff;
             margin-bottom: 25px;
               padding:30px 20px;
            box-sizing: border-box;;
        }
        .body{
            width: 100%;
            height:500px ;
            box-sizing: border-box;
            padding:10px 20px;
            overflow: auto;
            background-color: #fff;
        }
        

    }
</style>